package cn.yangyumanage.model;

import com.google.gson.reflect.TypeToken;

import java.io.IOException;

import cn.yangyumanage.application.DataApplication;
import cn.yangyumanage.bean.Film;
import cn.yangyumanage.bean.util.NormalContainer;
import cn.yangyumanage.bean.util.PageContainer;
import cn.yangyumanage.callback.DataCallBack;
import cn.yangyumanage.callback.HttpCallBack;
import cn.yangyumanage.config.IpConfig;
import cn.yangyumanage.http.HttpClient;
import cn.yangyumanage.util.Aes;

public class FilmManageHttpModel implements BaseModel{
    //影片id倒序分页查询
    public void getPage(int currentPage, int pageSize, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/films/" + currentPage + "/" + pageSize;
        HttpClient.asyncGet(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    TypeToken<PageContainer<Film>> typeToken = new TypeToken<PageContainer<Film>>() {
                    };
                    PageContainer<Film> container = DataApplication.gson.fromJson(decrypt, typeToken.getType());
                    dataCallBack.getData(container.getData());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.ADMIN);
    }

    //影片分页模糊查询
    public void fuzzyQuery(int currentPage, int pageSize, String queryContent, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/public/films/searchresults/" + currentPage + "/" + pageSize + "?queryContent=" + queryContent;
        HttpClient.asyncGet(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    TypeToken<PageContainer<Film>> typeToken = new TypeToken<PageContainer<Film>>() {
                    };
                    PageContainer<Film> container = DataApplication.gson.fromJson(decrypt,typeToken.getType());
                    dataCallBack.getData(container.getData());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.PUBLIC);
    }

    //通过id删除影片
    public void deleteFilmById(int id, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/films/" + id;
        HttpClient.asyncDelete(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    NormalContainer container = DataApplication.gson.fromJson(decrypt, NormalContainer.class);
                    dataCallBack.getData(container.getFlag());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.ADMIN);
    }

    @Override
    public void onDestroy() {

    }
}
